from socket import *
from time import ctime
import threading

HOST = ''
PORT = [21565,21566,21567]
ADDR = [(HOST, PORT[0]),(HOST, PORT[1]),(HOST, PORT[2])]
BUFSIZE = 1024

def ser(i):
    tcpSrvSock=socket(AF_INET,SOCK_STREAM)
    tcpSrvSock.bind(ADDR[i])
    tcpSrvSock.listen(2)
    while True:
        tcpCliSock, addr = tcpSrvSock.accept()
        print('... connected from: client',i+1)
        while True:
            data = tcpCliSock.recv(BUFSIZE)
            print([ctime()], ':', data)
            tcpCliSock.send(str.encode(ctime()+": a heartbeat from client "+str(i+1)+' reaches'))
        tcpCliSock.close()
    tcpSrvSock.close()

if __name__=="__main__":
    print('waiting for connection ...', ctime())
    for i in range(3):
        t=threading.Thread(target=ser,args=(i,))
        t.start()


